package cn.tedu.douguo.user.controller;


import cn.tedu.douguo.commons.web.JsonResult;
import cn.tedu.douguo.user.pojo.param.UserLoginInfoParam;
import cn.tedu.douguo.user.pojo.param.UserRegInfoParam;
import cn.tedu.douguo.user.pojo.vo.UserLoginInfoVO;
import cn.tedu.douguo.user.pojo.vo.UserNavInfoVO;
import cn.tedu.douguo.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Slf4j
@Valid
@Api( tags = "一（￣︶￣）↗　用户管理")
public class UserController {

    @Autowired
    private UserService service;

    //处理用户注册的请求
    @PostMapping("/register")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 100)
    public JsonResult register(UserRegInfoParam userRegInfoParam){
        log.debug("开始处理用户注册的请求...");
        service.register(userRegInfoParam);
        return JsonResult.ok();
    }

    //处理用户登录的请求

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 110)
    public JsonResult login(UserLoginInfoParam userLoginInfoParam) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginInfoParam);
        String jwt = service.login(userLoginInfoParam);
        return JsonResult.ok(jwt);
    }

        //    用户登出
        @PostMapping("/logout")
        @ApiOperation("user退出登录")
        @ApiOperationSupport(order = 120)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "jwt", value = "用户jwt", required = true, dataType = "long")
        })
        public JsonResult logout( @RequestParam  String jwt) {
            log.debug("开始处理【退出登录】的请求，参数：{}", jwt);
            service.logout(jwt);
            return JsonResult.ok();
        }


    //处理查询单个用户信息的请求
    @GetMapping("/getUserInfo")
    @ApiOperation("用户信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long")
    })
    public JsonResult getUserInfo(Long userId) {
        log.debug("开始处理【用户单个信息】的请求，参数：{}", userId);
        UserNavInfoVO info = service.getUserInfoById(userId);
        return JsonResult.ok(info);
    }

}
